package com.link.play.fit.ai.ui.guide

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import androidx.lifecycle.lifecycleScope
import com.link.play.fit.ai.bean.MealsTimeData
import com.link.play.fit.ai.databinding.FgEatMealsBinding
import com.link.play.fit.ai.kv.AuthCache.firstMeals
import com.link.play.fit.ai.kv.AuthCache.secondMeals
import com.link.play.fit.ai.kv.AuthCache.thirdMeals
import com.link.play.fit.ai.store.CloudStore
import com.link.play.fit.ai.store.CloudStore.DOC_MEALS_TIME_LIST_DATA
import com.link.play.fit.ai.ui.dialog.SetTimeDialog
import com.link.play.fit.ai.ui.guide.ac.GuidePbAc
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch


class EatMealsFg : BasePageFragment<FgEatMealsBinding>(FgEatMealsBinding::inflate) {

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        val userGuideState = (requireActivity() as GuidePbAc).viewModel.userGuideState
        lifecycleScope.launch {
            userGuideState.collectLatest {
                changeMealsUI(it.mealsTime)
            }
        }

        arrayOf(binding.ivAdd1, binding.ivEdit1).forEach { _view ->
            _view.setOnClickListener {
                SetTimeDialog(requireActivity()).showDialog(if (firstMeals.isEmpty()) "08:10:AM" else firstMeals) {
                    firstMeals = it
                    userGuideState.update { data ->
                        data.copy(mealsTime = listOf(firstMeals, secondMeals, thirdMeals))
                    }
                }
            }
        }


        arrayOf(binding.ivAdd2, binding.ivEdit2).forEach { _view ->
            _view.setOnClickListener {
                SetTimeDialog(requireActivity()).showDialog(if (secondMeals.isEmpty()) "11:45:AM" else secondMeals) {
                    secondMeals = it
                    val data = MealsTimeData(value = it, timeStamp = System.currentTimeMillis().toString())
                    CloudStore.addToList(
                        DOC_MEALS_TIME_LIST_DATA,
                        DOC_MEALS_TIME_LIST_DATA,
                        data.timeStamp,
                        data
                    )
                    userGuideState.update { data ->
                        data.copy(mealsTime = listOf(firstMeals, secondMeals, thirdMeals))
                    }
                }
            }
        }
        arrayOf(binding.ivAdd3, binding.ivEdit3).forEach { _view ->
            _view.setOnClickListener {
                SetTimeDialog(requireActivity()).showDialog(if (thirdMeals.isEmpty()) "06:10:PM" else thirdMeals) {
                    thirdMeals = it
                    userGuideState.update { data ->
                        data.copy(mealsTime = listOf(firstMeals, secondMeals, thirdMeals))
                    }
                }
            }
        }

    }

    @SuppressLint("SetTextI18n")
    private fun changeMealsUI(mealsTime: List<String>) {
        kotlin.runCatching {
            val mealsFirst = mealsTime[0]
            if (mealsFirst.isEmpty()) {
                binding.tvTime1.visibility = View.GONE
                binding.ivEdit1.visibility = View.GONE
                binding.ivAdd1.visibility = View.VISIBLE
            } else {
                binding.tvTime1.visibility = View.VISIBLE
                binding.ivEdit1.visibility = View.VISIBLE
                binding.ivAdd1.visibility = View.GONE

                val split = mealsFirst.split(":")
                val hour = split[0]
                val min = split[1]
                val apm = split[2]
                binding.tvTime1.text = "$hour:$min $apm"
            }

            val mealsSecond = mealsTime[1]
            if (mealsSecond.isEmpty()) {
                binding.tvTime2.visibility = View.GONE
                binding.ivEdit2.visibility = View.GONE
                binding.ivAdd2.visibility = View.VISIBLE
            } else {
                binding.tvTime2.visibility = View.VISIBLE
                binding.ivEdit2.visibility = View.VISIBLE
                binding.ivAdd2.visibility = View.GONE

                val split = mealsSecond.split(":")
                val hour = split[0]
                val min = split[1]
                val apm = split[2]
                binding.tvTime2.text = "$hour:$min $apm"
            }

            val mealsThird = mealsTime[2]
            if (mealsThird.isEmpty()) {
                binding.tvTime3.visibility = View.GONE
                binding.ivEdit3.visibility = View.GONE
                binding.ivAdd3.visibility = View.VISIBLE
            } else {
                binding.tvTime3.visibility = View.VISIBLE
                binding.ivEdit3.visibility = View.VISIBLE
                binding.ivAdd3.visibility = View.GONE

                val split = mealsThird.split(":")
                val hour = split[0]
                val min = split[1]
                val apm = split[2]
                binding.tvTime3.text = "$hour:$min $apm"
            }
        }
    }

}
